class GamesController < ApplicationController
  def index
  	@games = Game.all
  end

  def new
  	@game = Game.new
  end

  def create
  	@game = Game.new(params[:game])

	@home_team = Team.find(params[:home_team])
	@visiting_team = Team.find(params[:visiting_team])

	if @home_team and @visiting_team and @home_team.id != @visiting_team.id
		@game.home_team = @home_team
    @game.visiting_team = @visiting_team

		@game.save

		redirect_to game_url(@game)
	else
		redirect_to new_game_path
	end
  end

  def show
  	@game = Game.find(params[:id])
    
    @home_team = @game.home_team
    @home_team_first_half, @home_team_second_half = @game.team_halve_summary.find_all_by_team_id(@home_team.id, :order => 'halve')

    @visiting_team = @game.visiting_team
    @visting_team_first_half, @visiting_team_second_half = @game.team_halve_summary.find_all_by_team_id(@visiting_team.id, :order => 'halve')
  end

  def destroy
    @game = Game.find(params[:id])
    @game.destroy

    redirect_to games_path
  end
end
